package com.jxt.app.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jxt.app.entity.Apmessages;
import com.jxt.app.entity.ApmessagesExample;
import com.jxt.app.entity.ApreplyExample;
import com.jxt.app.entity.ApsharePost;
import com.jxt.app.entity.ApsharePostExample;
import com.jxt.app.service.ApReplyService;
import com.jxt.app.service.ApSharePostService;
import com.jxt.app.util.Page;

@Controller
public class ShareController {
	@Resource 
	private ApSharePostService apSharePostService;
	@Resource
	private ApReplyService apReplyService;
	
	/**
	 * 家长分享列表
	 * @param page
	 * @param pageSize
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/share/list/{page}/{pageSize}",method=RequestMethod.GET) 
	public String shareList(@PathVariable int page,@PathVariable int pageSize,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
	
		ApsharePostExample example=new ApsharePostExample();
		example.setId((long)0);
		example.setId_Indicator(ApsharePostExample.EXAMPLE_GREATER_THAN);
		Page apnewsList = null;
		try {
			apnewsList = apSharePostService.findListByExample(example, page, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map resultMap=new HashMap();
		resultMap.put("code", "1001");
		resultMap.put("list", apnewsList);
		model.addAttribute("result", resultMap);
		return "megList";
	}
	/**
	 * 根据ID查询分享主题
	 * @param id
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/share/dt/{id}",method=RequestMethod.GET) 
	public String shareDetail(@PathVariable long id,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
	
		ApsharePostExample example=new ApsharePostExample();
		example.setId(id);
		example.setId_Indicator(ApsharePostExample.EXAMPLE_EQUALS);
		List<ApsharePost> apnewsList=apSharePostService.listModel(example);
		
		ApreplyExample apreplyExample=new ApreplyExample();
		if(null!=apnewsList&&apnewsList.size()>0){
			apreplyExample.setShareId(apnewsList.get(0).getId());
			apreplyExample.setShareId_Indicator(ApreplyExample.EXAMPLE_EQUALS);
			try {
				apReplyService.findListByExample(apreplyExample, 1, 10);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		Map resultMap=new HashMap();
		resultMap.put("code", "1001");
		resultMap.put("list", apnewsList);
		model.addAttribute("result", resultMap);
		return "megList";
	}
	/**
	 * 根据分享ID查询回复分页接口
	 * @param id
	 * @param page
	 * @param pageSize
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/reply/{id}/{page}/{pageSize}",method=RequestMethod.GET)
	public String getReplyByshareId(@PathVariable long id,@PathVariable int page,@PathVariable int pageSize,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
		
			ApreplyExample apreplyExample=new ApreplyExample();
			Page replyPage=null;
			apreplyExample.setShareId(id);
			apreplyExample.setShareId_Indicator(ApreplyExample.EXAMPLE_EQUALS);
			try {
				replyPage=apReplyService.findListByExample(apreplyExample, 1, 10);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
			Map resultMap=new HashMap();
			resultMap.put("code", "1001");
			resultMap.put("list", replyPage);
			model.addAttribute("result", resultMap);
		return null;
	
	}
}
